<template>
  <div class="container-topic-que">
    <el-dialog
      :title="dialogConfig.name"
      :visible.sync="dialogVisible"
      @close="dialogVisible = false"
      append-to-body
    >
      <el-form ref="formRef" :model="formData" :rules="rules">
        <el-form-item label="一级内容标签" :label-width="'100px'" prop="uid">
          <el-select
            v-model="formData.tagNo"
            placeholder="请选择"
            @focus="handleFocus(1)"
          >
            <el-option
              v-for="item in optionsTag"
              :key="item.tagNo"
              :label="item.content"
              :value="item.tagNo"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" :label-width="'100px'" prop="name">
          <el-input v-model="formData.content" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { topicQueUpdateApi, topicQueAddApi, tagListApi } from '@api/whisper'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: Object
    },
    request: {
      type: Object
    },
    dialogConfig: {
      type: Object
    }
  },
  data() {
    return {
      posting: false,
      formData: this.setup(this.value),
      rules: {},
      optionsTag: []
    }
  },
  created() {},
  mounted() {},
  methods: {
    setup() {
      if (!this.value) {
        return {
          content: '',
          id: 0,
          tagNo: ''
        }
      } else {
        const clone = deepClone(this.value)
        return clone
      }
    },

    // 内容标签
    handleFocus(val) {
      tagListApi(val).then(res => {
        if (res) {
          this.optionsTag = res.data.list
        }
      })
    },

    handleConfirm() {
      this.posting = true
      let func = null
      if (this.formData.id) {
        func = topicQueUpdateApi
      } else {
        func = topicQueAddApi
      }
      const clone = deepClone(this.formData)
      func(clone)
        .then(res => {
          this.dialogVisible = false
          this.$emit('success') // 刷新
        })
        .catch(err => {
          this.$message('error')
        })
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.formData = this.setup()
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.container-topic-que {
}
</style>
